package com.crm.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * This class serves as the Base class for all other Daos - namely to hold
 * common methods that they might all use. Can be used for standard CRUD
 * operations.
 *
 * @author <a href="mailto:rock.huang@eduglobal.com">Rock Huang</a>
 */
public class BaseDao extends HibernateDaoSupport {
    protected final Log log = LogFactory.getLog(getClass());

    public List getObjects(Class clazz) {
        return getHibernateTemplate().loadAll(clazz);
    }

    public Object getObject(Class clazz, Serializable id) {
        Object o = getHibernateTemplate().get(clazz, id);
        if (o == null) {
            throw new ObjectRetrievalFailureException(clazz, id);
        }
        return null;
    }

    public void saveObject(Object o) {
        getHibernateTemplate().saveOrUpdate(o);
    }

    public void removeObject(Class clazz, Serializable id) {
        getHibernateTemplate().delete(getObject(clazz, id));
    }

}
